package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.domain.Song;
import com.hmusic.mymusic.domain.SongList;
import com.hmusic.mymusic.service.SongListService;
import com.hmusic.mymusic.utils.Consts;
import com.hmusic.mymusic.utils.DateUtil;
import com.hmusic.mymusic.utils.FileUpdateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 歌单Controller层
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/songList")
public class SongListController {

    @Autowired
    private SongListService songListService;
    @Autowired
    private FileUpdateUtil fileUpdateUtil;
    @Value("${songListPic.dir}")
    private String realPath;

    /**
     * 添加歌单
     */
    @PostMapping("/add")
    public Object addSongList(@RequestBody SongList songList){
        JSONObject jsonObject = new JSONObject();
        try{
            songListService.insert(songList);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加歌单成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"添加歌单失败");
            return jsonObject;
        }
    }

    /**
     * 修改歌单
     */
    @PostMapping("/update")
    public Object updateSongList(@RequestBody SongList songList){
        JSONObject jsonObject = new JSONObject();
        try{
            songListService.update(songList);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改歌单成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改歌单失败");
            return jsonObject;
        }
    }

    /**
     * 删除歌单
     */
    @GetMapping("/delete")
    @ResponseBody
    public Object deleteSongList(String id){
        JSONObject jsonObject = new JSONObject();
        try{
            songListService.delete(id);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除歌单成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除歌单失败");
            return jsonObject;
        }
    }

    /**
     * 根据主键查询整个对象
     */
    @GetMapping("/selectByPrimaryKey")
    @ResponseBody
    public Object selectByPrimaryKey(String id){
        return songListService.selectByPrimaryKey(id);
    }

    /**
     * 查询所有歌单
     */
    @GetMapping("/allSongList")
    @ResponseBody
    public Object allSongList(){
        return songListService.allSongList();
    }

    @GetMapping("/songListByStyle")
    @ResponseBody
    public Object songListByStyle(String style){
        return songListService.likeStyle(style);
    }

    /**
     * 首页热门歌单
     * @return
     */
    @GetMapping("/hotSongList")
    @ResponseBody
    public Object hotSongList(){
        return songListService.hotSongList();
    }

    /**
     * 点击量增加
     * @param id
     * @return
     */
    @GetMapping("/updateClick")
    @ResponseBody
    public Object updateClick(String id){
        JSONObject jsonObject = new JSONObject();
        try{
            songListService.updateClick(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * 上传/更新歌单图片
     */
    @PostMapping("/updateSongListPic")
    public Object updateSingerPic(@RequestParam("file") MultipartFile songListFile,String id){
        JSONObject jsonObject = new JSONObject();
        try{
            String storeSongListPicPath = fileUpdateUtil.updateSongListPic(id,songListFile);
            SongList songList1 = new SongList();
            songList1.setId(id);
            songList1.setPic(storeSongListPicPath);
            songListService.update(songList1);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"上传歌单图片成功");
            jsonObject.put("pic",storeSongListPicPath);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传歌单图片失败");
            return jsonObject;
        }
    }

    @GetMapping("/songListCount")
    @ResponseBody
    public Object songListCount(){
        return songListService.songListCount();
    }

}
